function results = make_SV(file_name,start_date,finish_date)

% ---------------------------------------------------------------------------------- %

% This function computes signed trading volumes for various intervals
% during the trading day

data = load(file_name);

% clean empty observations 
data.trades_per_min_mat(isnan(data.trades_per_min_mat))                     = 0 ;
data.trade_quantity_per_min_mat(isnan(data.trade_quantity_per_min_mat))     = 0 ;
data.signed_vol_per_min_mat(isnan(data.signed_vol_per_min_mat))             = 0 ;
No_trades                                                                   = data.trades_per_min_mat;
No_contracts                                                                = data.trade_quantity_per_min_mat;
Signed_volume                                                               = data.signed_vol_per_min_mat;
caldt_mat                                                                   = data.caldt_mat;

beg                 = find_closest(data.caldt_daily,start_date);
fin                 = find_closest(data.caldt_daily,finish_date);

caldt_trades        = data.caldt_daily(beg:fin);
No_trades           = No_trades(beg:fin,:);
Volume              = No_contracts(beg:fin,:);
Signed_volume       = Signed_volume(beg:fin,:);
caldt_mat           = caldt_mat(beg:fin,:);

% ----------------------------------------------------------------% 
% signed volume sampling
% ----------------------------------------------------------------% 

indx                        = [3 : 3 : length(Signed_volume(1,:))];
indx_loop                   = [0 , indx];
Signed_volume_15b15_AD      = nan(size(Signed_volume,1),24*4);
volume_15b15_AD             = nan(size(Signed_volume,1),24*4);

for H = 1 : round(length(Signed_volume(1,:))/12*4)
    Signed_volume_15b15_AD(:,H)         = sum(Signed_volume(:,indx_loop(H)+1 : indx_loop(H+1)),2);
    volume_15b15_AD(:,H)                = sum(Volume(:,indx_loop(H)+1 : indx_loop(H+1)),2);
    Time_of_day(1,H)                    = caldt_mat(1,indx_loop(H)+1);
    Time_of_day(2,H)                    = caldt_mat(1,indx_loop(H+1));
end

rel_Signed_volume_15b15_AD = Signed_volume_15b15_AD./volume_15b15_AD;
rel_Signed_volume_15b15_AD(isnan(rel_Signed_volume_15b15_AD)) = 0 ;

% ----------------------------------------------------------------% 
% quarter past the hour end of day imbalances 
% ----------------------------------------------------------------%

start_grid = 58 : 4 : 86;

% 86  -> 89 = 15:15   to 16:15 
% 82  -> 85 = 14:15   to 15:15 
% 78  -> 81 = 13:15   to 14:15 
% 74  -> 77 = 12:15   to 13:15 
% 70  -> 73 = 11:15   to 12:15 
% 66  -> 69 = 10:15   to 11:15 
% 62  -> 65 = 9:15    to 10:15
% 58  -> 61 = 8:15    to 9:15

for lag = 1 : length(start_grid)
        
    sum_signedVol_start              = start_grid(lag);
    sum_signedVol_end                = sum_signedVol_start+3;
    EOD_volume15(:,lag)              = sum(volume_15b15_AD(1:end,sum_signedVol_start:sum_signedVol_end),2);
    indx_no_trading_EOD_volume       = find(EOD_volume15(:,lag) < 100);
    EOD_signed_volume15(:,lag)       = sum(Signed_volume_15b15_AD(1:end,sum_signedVol_start:sum_signedVol_end),2);
    EOD_rel_signed_volume15(:,lag)   = EOD_signed_volume15(:,lag)./EOD_volume15(:,lag);
    
end

T                       = length(EOD_rel_signed_volume15);
EOD_volume15            = [NaN(T,24-length(start_grid)) , EOD_volume15];         % need to be the T x 24 matrices
EOD_signed_volume15     = [NaN(T,24-length(start_grid)) , EOD_signed_volume15];         % need to be the T x 24 matrices
EOD_rel_signed_volume15 = [NaN(T,24-length(start_grid)) , EOD_rel_signed_volume15];     % need to be the T x 24 matrices


% ----------------------------------------------------------------% 
% on the hour ALL day 
% ----------------------------------------------------------------%

start_grid = 1 : 4 : 93;

% 89  -> 92 = 16:00   to 17:00 
% 85  -> 88 = 15:00   to 16:00 
% 81  -> 84 = 14:00   to 15:00 
% 77  -> 80 = 13:00   to 14:00 
% 73  -> 76 = 12:00   to 13:00 
% 69  -> 72 = 11:00   to 12:00 
% 65  -> 68 = 10:00   to 11:00 
% 61  -> 64 = 9:00    to 10:00
% 57  -> 60 = 8:00    to 9:00

for lag = 1 : length(start_grid)
        
    sum_signedVol_start                 = start_grid(lag);
    sum_signedVol_end                   = sum_signedVol_start+3;
    EOD_volume(:,lag)                   = sum(volume_15b15_AD(1:end,sum_signedVol_start:sum_signedVol_end),2);
    indx_no_trading_EOD_volume          = find(EOD_volume(:,lag) < 100);
    EOD_signed_volume(:,lag)            = sum(Signed_volume_15b15_AD(1:end,sum_signedVol_start:sum_signedVol_end),2);
    EOD_rel_signed_volume(:,lag)        = EOD_signed_volume(:,lag)./EOD_volume(:,lag);
    
end

% ----------------------------------------------------------------% 
% save output  
% ----------------------------------------------------------------%

results.caldt                            = caldt_trades;
results.No_trades                        = No_trades;
results.Volume                           = Volume;
results.Signed_volume                    = Signed_volume;

results.volume_15b15_AD                  = volume_15b15_AD;
results.Signed_volume_15b15_AD           = Signed_volume_15b15_AD;           
results.rel_Signed_volume_15b15_AD       = rel_Signed_volume_15b15_AD;

results.EOD_volume15                     = EOD_volume15;
results.EOD_signed_volume15              = EOD_signed_volume15;
results.EOD_rel_signed_volume15          = EOD_rel_signed_volume15;

results.EOD_volume                       = EOD_volume;
results.EOD_signed_volume                = EOD_signed_volume;
results.EOD_rel_signed_volume            = EOD_rel_signed_volume;
results.Time_of_day                      = Time_of_day;

return;


